<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>FabGL: VGA/PCEmulator/PCEmulator.ino</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">FabGL
   </div>
   <div id="projectbrief">ESP32 Display Controller and Graphics Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('_v_g_a_2_p_c_emulator_2_p_c_emulator_8ino-example.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle"><div class="title">VGA/PCEmulator/PCEmulator.ino</div></div>
</div><!--header-->
<div class="contents">
<p >A i8086 based IBM PC emulator (runs FreeDOS, MS-DOS, CPM86, Linux-ELK, Windows 3.0)</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - &lt;http://www.fabgl.com&gt;</span></div>
<div class="line"><span class="comment">  Copyright (c) 2019-2022 Fabrizio Di Vittorio.</span></div>
<div class="line"><span class="comment">  All rights reserved.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">* Please contact fdivitto2013@gmail.com if you need a commercial license.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">* This library and related software is available under GPL v3.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  FabGL is free software: you can redistribute it and/or modify</span></div>
<div class="line"><span class="comment">  it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><span class="comment">  the Free Software Foundation, either version 3 of the License, or</span></div>
<div class="line"><span class="comment">  (at your option) any later version.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  FabGL is distributed in the hope that it will be useful,</span></div>
<div class="line"><span class="comment">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><span class="comment">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><span class="comment">  GNU General Public License for more details.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  You should have received a copy of the GNU General Public License</span></div>
<div class="line"><span class="comment">  along with FabGL.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> <span class="comment">/* Instructions:</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">    - to run this application you need an ESP32 with PSRAM installed and an SD-CARD slot (ie TTGO VGA32 v1.4 or FabGL Development Board with WROVER)</span></div>
<div class="line"><span class="comment">    - open this with Arduino and make sure PSRAM is DISABLED</span></div>
<div class="line"><span class="comment">    - partition scheme must be: Huge App</span></div>
<div class="line"><span class="comment">    - compile and upload the sketch</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#pragma message &quot;This sketch requires Tools-&gt;Partition Scheme = Huge APP&quot;</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;memory&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;esp32-hal-psram.h&quot;</span></div>
<div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div>
<div class="line"><span class="preprocessor">#include &quot;esp_spiram.h&quot;</span></div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#include &quot;esp_sntp.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;Preferences.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;WiFi.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;HTTPClient.h&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="fabgl_8h.html">fabgl.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;mconf.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;machine.h&quot;</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// UART Pins for USB serial</span></div>
<div class="line"><span class="preprocessor">#define UART_URX 3</span></div>
<div class="line"><span class="preprocessor">#define UART_UTX 1</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> std::unique_ptr;</div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> fabgl::StringList;</div>
<div class="line"><span class="keyword">using</span> fabgl::imin;</div>
<div class="line"><span class="keyword">using</span> fabgl::imax;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">Preferences   preferences;</div>
<div class="line">InputBox      ibox;</div>
<div class="line">Machine     * machine;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// noinit! Used to maintain datetime between reboots</span></div>
<div class="line">__NOINIT_ATTR <span class="keyword">static</span> timeval savedTimeValue;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">bool</span> wifiConnected = <span class="keyword">false</span>;</div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">bool</span> downloadOK    = <span class="keyword">false</span>;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// try to connected using saved parameters</span></div>
<div class="line"><span class="keywordtype">bool</span> tryToConnect()</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">bool</span> connected = WiFi.status() == WL_CONNECTED;</div>
<div class="line">  <span class="keywordflow">if</span> (!connected) {</div>
<div class="line">    <span class="keywordtype">char</span> SSID[32] = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line">    <span class="keywordtype">char</span> psw[32]  = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line">    <span class="keywordflow">if</span> (preferences.getString(<span class="stringliteral">&quot;SSID&quot;</span>, SSID, <span class="keyword">sizeof</span>(SSID)) &amp;&amp; preferences.getString(<span class="stringliteral">&quot;WiFiPsw&quot;</span>, psw, <span class="keyword">sizeof</span>(psw))) {</div>
<div class="line">      ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Abort&quot;</span>, <span class="keyword">true</span>, 200, [&amp;](fabgl::ProgressForm * form) {</div>
<div class="line">        WiFi.begin(SSID, psw);</div>
<div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 32 &amp;&amp; WiFi.status() != WL_CONNECTED; ++i) {</div>
<div class="line">          <span class="keywordflow">if</span> (!form-&gt;update(i * 100 / 32, <span class="stringliteral">&quot;Connecting to %s...&quot;</span>, SSID))</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">          delay(500);</div>
<div class="line">          <span class="keywordflow">if</span> (i == 16)</div>
<div class="line">            WiFi.reconnect();</div>
<div class="line">        }</div>
<div class="line">        connected = (WiFi.status() == WL_CONNECTED);</div>
<div class="line">      });</div>
<div class="line">      <span class="comment">// show to user the connection state</span></div>
<div class="line">      <span class="keywordflow">if</span> (!connected) {</div>
<div class="line">        WiFi.disconnect();</div>
<div class="line">        ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;WiFi Connection failed!&quot;</span>);</div>
<div class="line">      }</div>
<div class="line">    }</div>
<div class="line">  }</div>
<div class="line">  <span class="keywordflow">return</span> connected;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> checkWiFi()</div>
<div class="line">{</div>
<div class="line">  wifiConnected = tryToConnect();</div>
<div class="line">  <span class="keywordflow">if</span> (!wifiConnected) {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// configure WiFi?</span></div>
<div class="line">    <span class="keywordflow">if</span> (ibox.message(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Configure WiFi?&quot;</span>, <span class="stringliteral">&quot;No&quot;</span>, <span class="stringliteral">&quot;Yes&quot;</span>) == InputResult::Enter) {</div>
<div class="line"> </div>
<div class="line">      <span class="comment">// repeat until connected or until user cancels</span></div>
<div class="line">      <span class="keywordflow">do</span> {</div>
<div class="line"> </div>
<div class="line">        <span class="comment">// yes, scan for networks showing a progress dialog box</span></div>
<div class="line">        <span class="keywordtype">int</span> networksCount = 0;</div>
<div class="line">        ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">false</span>, 200, [&amp;](fabgl::ProgressForm * form) {</div>
<div class="line">          form-&gt;update(0, <span class="stringliteral">&quot;Scanning WiFi networks...&quot;</span>);</div>
<div class="line">          networksCount = WiFi.scanNetworks();</div>
<div class="line">        });</div>
<div class="line"> </div>
<div class="line">        <span class="comment">// are there available WiFi?</span></div>
<div class="line">        <span class="keywordflow">if</span> (networksCount &gt; 0) {</div>
<div class="line"> </div>
<div class="line">          <span class="comment">// yes, show a selectable list</span></div>
<div class="line">          StringList list;</div>
<div class="line">          <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; networksCount; ++i)</div>
<div class="line">            list.appendFmt(<span class="stringliteral">&quot;%s (%d dBm)&quot;</span>, WiFi.SSID(i).c_str(), WiFi.RSSI(i));</div>
<div class="line">          <span class="keywordtype">int</span> s = ibox.menu(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Please select a WiFi network&quot;</span>, &amp;list);</div>
<div class="line"> </div>
<div class="line">          <span class="comment">// user selected something?</span></div>
<div class="line">          <span class="keywordflow">if</span> (s &gt; -1) {</div>
<div class="line">            <span class="comment">// yes, ask for WiFi password</span></div>
<div class="line">            <span class="keywordtype">char</span> psw[32] = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line">            <span class="keywordflow">if</span> (ibox.textInput(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Insert WiFi password&quot;</span>, psw, 31, <span class="stringliteral">&quot;Cancel&quot;</span>, <span class="stringliteral">&quot;OK&quot;</span>, <span class="keyword">true</span>) == InputResult::Enter) {</div>
<div class="line">              <span class="comment">// user pressed OK, connect to WiFi...</span></div>
<div class="line">              preferences.putString(<span class="stringliteral">&quot;SSID&quot;</span>, WiFi.SSID(s).c_str());</div>
<div class="line">              preferences.putString(<span class="stringliteral">&quot;WiFiPsw&quot;</span>, psw);</div>
<div class="line">              wifiConnected = tryToConnect();</div>
<div class="line">              <span class="comment">// show to user the connection state</span></div>
<div class="line">              <span class="keywordflow">if</span> (wifiConnected)</div>
<div class="line">                ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Connection succeeded!&quot;</span>);</div>
<div class="line">            } <span class="keywordflow">else</span></div>
<div class="line">              <span class="keywordflow">break</span>;</div>
<div class="line">          } <span class="keywordflow">else</span></div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        } <span class="keywordflow">else</span> {</div>
<div class="line">          <span class="comment">// there is no WiFi</span></div>
<div class="line">          ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;No WiFi network found!&quot;</span>);</div>
<div class="line">          <span class="keywordflow">break</span>;</div>
<div class="line">        }</div>
<div class="line">        WiFi.scanDelete();</div>
<div class="line"> </div>
<div class="line">      } <span class="keywordflow">while</span> (!wifiConnected);</div>
<div class="line"> </div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">return</span> wifiConnected;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// handle soft restart</span></div>
<div class="line"><span class="keywordtype">void</span> shutdownHandler()</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// save current datetime into Preferences</span></div>
<div class="line">  gettimeofday(&amp;savedTimeValue, <span class="keyword">nullptr</span>);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> updateDateTime()</div>
<div class="line">{</div>
<div class="line">  <span class="comment">// Set timezone</span></div>
<div class="line">  setenv(<span class="stringliteral">&quot;TZ&quot;</span>, <span class="stringliteral">&quot;CET-1CEST,M3.5.0,M10.5.0/3&quot;</span>, 1);</div>
<div class="line">  tzset();</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// get datetime from savedTimeValue? (noinit section)</span></div>
<div class="line">  <span class="keywordflow">if</span> (esp_reset_reason() == ESP_RST_SW) {</div>
<div class="line">    <span class="comment">// adjust time taking account elapsed time since ESP32 started</span></div>
<div class="line">    savedTimeValue.tv_usec += (int) esp_timer_get_time();</div>
<div class="line">    savedTimeValue.tv_sec  += savedTimeValue.tv_usec / 1000000;</div>
<div class="line">    savedTimeValue.tv_usec %= 1000000;</div>
<div class="line">    settimeofday(&amp;savedTimeValue, <span class="keyword">nullptr</span>);</div>
<div class="line">    <span class="keywordflow">return</span>;</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">if</span> (checkWiFi()) {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// we need time right now</span></div>
<div class="line">    ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">true</span>, 200, [&amp;](fabgl::ProgressForm * form) {</div>
<div class="line">      sntp_setoperatingmode(SNTP_OPMODE_POLL);</div>
<div class="line">      sntp_setservername(0, (<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;pool.ntp.org&quot;</span>);</div>
<div class="line">      sntp_init();</div>
<div class="line">      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 12 &amp;&amp; sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED; ++i) {</div>
<div class="line">        form-&gt;update(i * 100 / 12, <span class="stringliteral">&quot;Getting date-time from SNTP...&quot;</span>);</div>
<div class="line">        delay(500);</div>
<div class="line">      }</div>
<div class="line">      sntp_stop();</div>
<div class="line">      ibox.setAutoOK(2);</div>
<div class="line">      ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Date and Time updated. Restarting...&quot;</span>);</div>
<div class="line"><span class="preprocessor">      #ifndef FABGL_EMULATED</span></div>
<div class="line">      esp_restart();</div>
<div class="line"><span class="preprocessor">      #endif</span></div>
<div class="line">    });</div>
<div class="line"> </div>
<div class="line">  } <span class="keywordflow">else</span> {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// set default time</span></div>
<div class="line">    <span class="keyword">auto</span> tm = (<span class="keyword">struct </span>tm){ .tm_sec  = 0, .tm_min  = 0, .tm_hour = 8, .tm_mday = 14, .tm_mon  = 7, .tm_year = 84 };</div>
<div class="line">    <span class="keyword">auto</span> now = (timeval){ .tv_sec = mktime(&amp;tm) };</div>
<div class="line">    settimeofday(&amp;now, <span class="keyword">nullptr</span>);</div>
<div class="line"> </div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// download specified filename from URL</span></div>
<div class="line"><span class="keywordtype">bool</span> downloadURL(<span class="keywordtype">char</span> <span class="keyword">const</span> * URL, FILE * file)</div>
<div class="line">{</div>
<div class="line">  downloadOK = <span class="keyword">false</span>;</div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * filename = strrchr(URL, <span class="charliteral">&#39;/&#39;</span>) + 1;</div>
<div class="line"> </div>
<div class="line">  ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Abort&quot;</span>, <span class="keyword">true</span>, 380, [&amp;](fabgl::ProgressForm * form) {</div>
<div class="line">    form-&gt;update(0, <span class="stringliteral">&quot;Preparing to download %s&quot;</span>, filename);</div>
<div class="line">    HTTPClient http;</div>
<div class="line">    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);</div>
<div class="line">    http.begin(URL);</div>
<div class="line">    <span class="keywordtype">int</span> httpCode = http.GET();</div>
<div class="line">    <span class="keywordflow">if</span> (httpCode == HTTP_CODE_OK) {</div>
<div class="line">      <span class="keywordflow">if</span> (file) {</div>
<div class="line">        <span class="keywordtype">int</span> tlen = http.getSize();</div>
<div class="line">        <span class="keywordtype">int</span> len = tlen;</div>
<div class="line">        <span class="keyword">auto</span> buf = (uint8_t*) SOC_EXTRAM_DATA_LOW; <span class="comment">// use PSRAM as buffer</span></div>
<div class="line">        WiFiClient * stream = http.getStreamPtr();</div>
<div class="line">        <span class="keywordtype">int</span> dsize = 0;</div>
<div class="line">        <span class="keywordflow">while</span> (http.connected() &amp;&amp; (len &gt; 0 || len == -1)) {</div>
<div class="line">          <span class="keywordtype">size_t</span> size = stream-&gt;available();</div>
<div class="line">          <span class="keywordflow">if</span> (size) {</div>
<div class="line">            <span class="keywordtype">int</span> c = stream-&gt;readBytes(buf, size);</div>
<div class="line">            <span class="keyword">auto</span> wr = fwrite(buf, 1, c, file);</div>
<div class="line">            <span class="keywordflow">if</span> (wr != c) {</div>
<div class="line">              dsize = 0;</div>
<div class="line">              <span class="keywordflow">break</span>;  <span class="comment">// writing failure!</span></div>
<div class="line">            }</div>
<div class="line">            dsize += c;</div>
<div class="line">            <span class="keywordflow">if</span> (len &gt; 0)</div>
<div class="line">              len -= c;</div>
<div class="line">            <span class="keywordflow">if</span> (!form-&gt;update((int64_t)dsize * 100 / tlen, <span class="stringliteral">&quot;Downloading %s (%.2f / %.2f MB)&quot;</span>, filename, (<span class="keywordtype">double</span>)dsize / 1048576.0, tlen / 1048576.0))</div>
<div class="line">              <span class="keywordflow">break</span>;</div>
<div class="line">          }</div>
<div class="line">        }</div>
<div class="line">        downloadOK = (len == 0 || (len == -1 &amp;&amp; dsize &gt; 0));</div>
<div class="line">      }</div>
<div class="line">    }</div>
<div class="line">    http.end();</div>
<div class="line">  });</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">return</span> downloadOK;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// return filename if successfully downloaded or already exist</span></div>
<div class="line"><span class="keywordtype">char</span> <span class="keyword">const</span> * getDisk(<span class="keywordtype">char</span> <span class="keyword">const</span> * url)</div>
<div class="line">{</div>
<div class="line">  FileBrowser fb(SD_MOUNT_PATH);</div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * filename = <span class="keyword">nullptr</span>;</div>
<div class="line">  <span class="keywordflow">if</span> (url) {</div>
<div class="line">    <span class="keywordflow">if</span> (strncmp(<span class="stringliteral">&quot;http://&quot;</span>, url, 7) == 0 || strncmp(<span class="stringliteral">&quot;https://&quot;</span>, url, 7) == 0) {</div>
<div class="line">      <span class="comment">// this is actually an URL</span></div>
<div class="line">      filename = strrchr(url, <span class="charliteral">&#39;/&#39;</span>) + 1;</div>
<div class="line">      <span class="keywordflow">if</span> (filename &amp;&amp; !fb.exists(filename, <span class="keyword">false</span>)) {</div>
<div class="line">        <span class="comment">// disk doesn&#39;t exist, try to download</span></div>
<div class="line">        <span class="keywordflow">if</span> (!checkWiFi())</div>
<div class="line">          <span class="keywordflow">return</span> <span class="keyword">nullptr</span>;</div>
<div class="line">        <span class="keyword">auto</span> file = fb.openFile(filename, <span class="stringliteral">&quot;wb&quot;</span>);</div>
<div class="line">        <span class="keywordtype">bool</span> success = downloadURL(url, file);</div>
<div class="line">        fclose(file);</div>
<div class="line">        <span class="keywordflow">if</span> (!success) {</div>
<div class="line">          fb.remove(filename);</div>
<div class="line">          <span class="keywordflow">return</span> <span class="keyword">nullptr</span>;</div>
<div class="line">        }</div>
<div class="line">      }</div>
<div class="line">    } <span class="keywordflow">else</span> {</div>
<div class="line">      <span class="comment">// this is just a file</span></div>
<div class="line">      <span class="keywordflow">if</span> (fb.filePathExists(url))</div>
<div class="line">        filename = url;</div>
<div class="line">    }</div>
<div class="line">  }</div>
<div class="line">  <span class="keywordflow">return</span> filename;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// user pressed SYSREQ (ALT + PRINTSCREEN)</span></div>
<div class="line"><span class="keywordtype">void</span> sysReqCallback()</div>
<div class="line">{</div>
<div class="line">  machine-&gt;graphicsAdapter()-&gt;enableVideo(<span class="keyword">false</span>);</div>
<div class="line">  ibox.begin(<a id="a0" name="a0"></a><a class="code hl_define" href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a>, 500, 400, 4);</div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">int</span> s = ibox.menu(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Select a command&quot;</span>, <span class="stringliteral">&quot;Restart Emulator;Restart Machine;Mount Disk;Continue&quot;</span>);</div>
<div class="line">  <span class="keywordflow">switch</span> (s) {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Restart Emulator</span></div>
<div class="line">    <span class="keywordflow">case</span> 0:</div>
<div class="line">      esp_restart();</div>
<div class="line">      <span class="keywordflow">break</span>;</div>
<div class="line">      </div>
<div class="line">    <span class="comment">// Restart Machine</span></div>
<div class="line">    <span class="keywordflow">case</span> 1:</div>
<div class="line">      machine-&gt;trigReset();</div>
<div class="line">      <span class="keywordflow">break</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Mount Disk</span></div>
<div class="line">    <span class="keywordflow">case</span> 2:</div>
<div class="line">    {</div>
<div class="line">      <span class="keywordtype">int</span> s = ibox.menu(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Select Drive&quot;</span>, <span class="stringliteral">&quot;Floppy A (fd0);Floppy B (fd1)&quot;</span>);</div>
<div class="line">      <span class="keywordflow">if</span> (s &gt; -1) {</div>
<div class="line">        <span class="keyword">constexpr</span> <span class="keywordtype">int</span> MAXNAMELEN = 256;</div>
<div class="line">        unique_ptr&lt;char[]&gt; dir(<span class="keyword">new</span> <span class="keywordtype">char</span>[MAXNAMELEN + 1] { <span class="charliteral">&#39;/&#39;</span>, <span class="charliteral">&#39;S&#39;</span>, <span class="charliteral">&#39;D&#39;</span>, 0 } );</div>
<div class="line">        unique_ptr&lt;char[]&gt; filename(<span class="keyword">new</span> <span class="keywordtype">char</span>[MAXNAMELEN + 1] { 0 } );</div>
<div class="line">        <span class="keywordflow">if</span> (machine-&gt;diskFilename(s))</div>
<div class="line">          strcpy(filename.get(), machine-&gt;diskFilename(s));</div>
<div class="line">        <span class="keywordflow">if</span> (ibox.fileSelector(<span class="stringliteral">&quot;Select Disk Image&quot;</span>, <span class="stringliteral">&quot;Image Filename&quot;</span>, dir.get(), MAXNAMELEN, filename.get(), MAXNAMELEN) == InputResult::Enter) {</div>
<div class="line">          machine-&gt;setDriveImage(s, filename.get());</div>
<div class="line">        }</div>
<div class="line">      }</div>
<div class="line">      <span class="keywordflow">break</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Continue</span></div>
<div class="line">    <span class="keywordflow">default</span>:</div>
<div class="line">      <span class="keywordflow">break</span>;</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  ibox.end();</div>
<div class="line">  PS2Controller::keyboard()-&gt;enableVirtualKeys(<span class="keyword">false</span>, <span class="keyword">false</span>); <span class="comment">// don&#39;t use virtual keys</span></div>
<div class="line">  machine-&gt;graphicsAdapter()-&gt;enableVideo(<span class="keyword">true</span>);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> setup()</div>
<div class="line">{</div>
<div class="line">  Serial.begin(115200); delay(500); printf(<span class="stringliteral">&quot;\n\n\nReset\n\n&quot;</span>);<span class="comment">// DEBUG ONLY</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">  disableCore0WDT();</div>
<div class="line">  delay(100); <span class="comment">// experienced crashes without this delay!</span></div>
<div class="line">  disableCore1WDT();</div>
<div class="line"> </div>
<div class="line">  preferences.begin(<span class="stringliteral">&quot;PCEmulator&quot;</span>, <span class="keyword">false</span>);</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// uncomment to clear preferences</span></div>
<div class="line">  <span class="comment">//preferences.clear();</span></div>
<div class="line">  </div>
<div class="line">  <span class="comment">// save some space reducing UI queue</span></div>
<div class="line">  <a id="a1" name="a1"></a><a class="code hl_variable" href="classfabgl_1_1_bitmapped_display_controller_a6b94fdf3692083a41291eec4920c62d2.html#a6b94fdf3692083a41291eec4920c62d2">fabgl::BitmappedDisplayController::queueSize</a> = 128;</div>
<div class="line"> </div>
<div class="line">  ibox.begin(<a class="code hl_define" href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a>, 500, 400, 4);</div>
<div class="line">  ibox.setBackgroundColor(RGB888(0, 0, 0));</div>
<div class="line"> </div>
<div class="line">  ibox.onPaint = [&amp;](Canvas * canvas) { drawInfo(canvas); };</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// we need PSRAM for this app, but we will handle it manually, so please DO NOT enable PSRAM on your development env</span></div>
<div class="line"><span class="preprocessor">  #ifdef BOARD_HAS_PSRAM</span></div>
<div class="line">  ibox.message(<span class="stringliteral">&quot;Warning!&quot;</span>, <span class="stringliteral">&quot;Please disable PSRAM to improve performance!&quot;</span>);</div>
<div class="line"><span class="preprocessor">  #endif</span></div>
<div class="line"> </div>
<div class="line">  <span class="comment">// note: we use just 2MB of PSRAM so the infamous PSRAM bug should not happen. But to avoid gcc compiler hack (-mfix-esp32-psram-cache-issue)</span></div>
<div class="line">  <span class="comment">// we enable PSRAM at runtime, otherwise the hack slows down CPU too much (PSRAM_HACK is no more required).</span></div>
<div class="line">  <span class="keywordflow">if</span> (esp_spiram_init() != ESP_OK)</div>
<div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;This app requires a board with PSRAM!&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">nullptr</span>);</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">  #ifndef BOARD_HAS_PSRAM</span></div>
<div class="line">  esp_spiram_init_cache();</div>
<div class="line"><span class="preprocessor">  #endif</span></div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">if</span> (!FileBrowser::mountSDCard(<span class="keyword">false</span>, SD_MOUNT_PATH, 8))   <span class="comment">// @TODO: reduce to 4?</span></div>
<div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;This app requires a SD-CARD!&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">nullptr</span>);</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// uncomment to format SD!</span></div>
<div class="line">  <span class="comment">//FileBrowser::format(fabgl::DriveType::SDCard, 0);</span></div>
<div class="line"> </div>
<div class="line">  esp_register_shutdown_handler(shutdownHandler);</div>
<div class="line"> </div>
<div class="line">  updateDateTime();</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// machine configurations</span></div>
<div class="line">  MachineConf mconf;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// show a list of machine configurations</span></div>
<div class="line"> </div>
<div class="line">  ibox.setAutoOK(6);</div>
<div class="line">  <span class="keywordtype">int</span> idx = preferences.getInt(<span class="stringliteral">&quot;dconf&quot;</span>, 0);</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">bool</span> showDialog = <span class="keyword">true</span>; showDialog; ) {</div>
<div class="line"> </div>
<div class="line">    loadMachineConfiguration(&amp;mconf);</div>
<div class="line"> </div>
<div class="line">    StringList dconfs;</div>
<div class="line">    <span class="keywordflow">for</span> (<span class="keyword">auto</span> conf = mconf.getFirstItem(); conf; conf = conf-&gt;next)</div>
<div class="line">      dconfs.append(conf-&gt;desc);</div>
<div class="line">    dconfs.select(idx, <span class="keyword">true</span>);</div>
<div class="line"> </div>
<div class="line">    ibox.setupButton(0, <span class="stringliteral">&quot;Files&quot;</span>);</div>
<div class="line">    ibox.setupButton(1, <span class="stringliteral">&quot;Machine&quot;</span>, <span class="stringliteral">&quot;Edit;New;Remove&quot;</span>, 52);</div>
<div class="line">    <span class="keyword">auto</span> r = ibox.select(<span class="stringliteral">&quot;Machine Configurations&quot;</span>, <span class="stringliteral">&quot;Please select a machine configuration&quot;</span>, &amp;dconfs, <span class="keyword">nullptr</span>, <span class="stringliteral">&quot;Run&quot;</span>);</div>
<div class="line"> </div>
<div class="line">    idx = dconfs.getFirstSelected();</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">switch</span> (r) {</div>
<div class="line">      <span class="keywordflow">case</span> InputResult::ButtonExt0:</div>
<div class="line">        <span class="comment">// Browse Files</span></div>
<div class="line">        ibox.folderBrowser(<span class="stringliteral">&quot;Browse Files&quot;</span>, SD_MOUNT_PATH);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">case</span> InputResult::ButtonExt1:</div>
<div class="line">        <span class="comment">// Machine</span></div>
<div class="line">        <span class="keywordflow">switch</span> (ibox.selectedSubItem()) {</div>
<div class="line">          <span class="comment">// Edit</span></div>
<div class="line">          <span class="keywordflow">case</span> 0:</div>
<div class="line">            editConfigDialog(&amp;ibox, &amp;mconf, idx);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">          <span class="comment">// New</span></div>
<div class="line">          <span class="keywordflow">case</span> 1:</div>
<div class="line">            newConfigDialog(&amp;ibox, &amp;mconf, idx);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">          <span class="comment">// Remove</span></div>
<div class="line">          <span class="keywordflow">case</span> 2:</div>
<div class="line">            delConfigDialog(&amp;ibox, &amp;mconf, idx);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        };</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">case</span> InputResult::Enter:</div>
<div class="line">        <span class="comment">// Run</span></div>
<div class="line">        showDialog = <span class="keyword">false</span>;</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">default</span>:</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// next selection will not have timeout</span></div>
<div class="line">    ibox.setAutoOK(0);</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  idx = imax(idx, 0);</div>
<div class="line">  preferences.putInt(<span class="stringliteral">&quot;dconf&quot;</span>, idx);</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// setup selected configuration</span></div>
<div class="line">  <span class="keyword">auto</span> conf = mconf.getItem(idx);</div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * diskFilename[DISKCOUNT];</div>
<div class="line">  downloadOK = <span class="keyword">true</span>;</div>
<div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; DISKCOUNT &amp;&amp; downloadOK; ++i)</div>
<div class="line">    diskFilename[i] = getDisk(conf-&gt;disk[i]);</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">if</span> (!downloadOK || (!diskFilename[0] &amp;&amp; !diskFilename[2])) {</div>
<div class="line">    <span class="comment">// unable to get boot disks</span></div>
<div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;Unable to get system disks!&quot;</span>);</div>
<div class="line">    esp_restart();</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">if</span> (wifiConnected) {</div>
<div class="line">    <span class="comment">// disk downloaded from the Internet, need to reboot to fully disable wifi</span></div>
<div class="line">    ibox.setAutoOK(2);</div>
<div class="line">    ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Disks downloaded. Restarting...&quot;</span>);</div>
<div class="line"><span class="preprocessor">    #ifndef FABGL_EMULATED</span></div>
<div class="line">    esp_restart();</div>
<div class="line"><span class="preprocessor">    #endif</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  ibox.end();</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// without WiFi it is possible to increase SD card speed</span></div>
<div class="line">  FileBrowser::setSDCardMaxFreqKHz(SDMMC_FREQ_DEFAULT);</div>
<div class="line">  FileBrowser::remountSDCard();</div>
<div class="line"> </div>
<div class="line">  machine = <span class="keyword">new</span> Machine;</div>
<div class="line"> </div>
<div class="line">  machine-&gt;setBaseDirectory(SD_MOUNT_PATH);</div>
<div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; DISKCOUNT; ++i)</div>
<div class="line">    machine-&gt;setDriveImage(i, diskFilename[i], conf-&gt;cylinders[i], conf-&gt;heads[i], conf-&gt;sectors[i]);</div>
<div class="line"> </div>
<div class="line">  machine-&gt;setBootDrive(conf-&gt;bootDrive);</div>
<div class="line">  </div>
<div class="line">  <span class="keyword">auto</span> serial1 = <span class="keyword">new</span> SerialPort;</div>
<div class="line">  serial1-&gt;setSignals(UART_URX, UART_UTX);</div>
<div class="line">  machine-&gt;setCOM1(serial1);</div>
<div class="line"> </div>
<div class="line">  <span class="comment">/*</span></div>
<div class="line"><span class="comment">  printf(&quot;MALLOC_CAP_32BIT : %d bytes (largest %d bytes)\r\n&quot;, heap_caps_get_free_size(MALLOC_CAP_32BIT), heap_caps_get_largest_free_block(MALLOC_CAP_32BIT));</span></div>
<div class="line"><span class="comment">  printf(&quot;MALLOC_CAP_8BIT  : %d bytes (largest %d bytes)\r\n&quot;, heap_caps_get_free_size(MALLOC_CAP_8BIT), heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));</span></div>
<div class="line"><span class="comment">  printf(&quot;MALLOC_CAP_DMA   : %d bytes (largest %d bytes)\r\n\n&quot;, heap_caps_get_free_size(MALLOC_CAP_DMA), heap_caps_get_largest_free_block(MALLOC_CAP_DMA));</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">  heap_caps_dump_all();</span></div>
<div class="line"><span class="comment">  */</span></div>
<div class="line"> </div>
<div class="line">  machine-&gt;setSysReqCallback(sysReqCallback);</div>
<div class="line"> </div>
<div class="line">  machine-&gt;run();</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK</span></div>
<div class="line"><span class="keyword">namespace </span>fabgl {</div>
<div class="line">  <span class="keyword">extern</span> <span class="keyword">volatile</span> uint64_t s_vgapalctrlcycles;</div>
<div class="line">}</div>
<div class="line"><span class="keyword">using</span> fabgl::s_vgapalctrlcycles;</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> loop()</div>
<div class="line">{</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK</span></div>
<div class="line">  <span class="keyword">static</span> uint32_t tcpu = 0, s1 = 0, count = 0;</div>
<div class="line">  tcpu = machine-&gt;ticksCounter();</div>
<div class="line">  s_vgapalctrlcycles = 0;</div>
<div class="line">  s1 = fabgl::getCycleCount();</div>
<div class="line">  delay(1000);</div>
<div class="line">  printf(<span class="stringliteral">&quot;%d\tCPU: %d&quot;</span>, count, machine-&gt;ticksCounter() - tcpu);</div>
<div class="line">  printf(<span class="stringliteral">&quot;   Graph: %lld / %d   (%d%%)\n&quot;</span>, s_vgapalctrlcycles, fabgl::getCycleCount() - s1, (<span class="keywordtype">int</span>)((<span class="keywordtype">double</span>)s_vgapalctrlcycles/240000000*100));</div>
<div class="line">  ++count;</div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"> </div>
<div class="line">  vTaskDelete(NULL);</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="ttc" id="aclassfabgl_1_1_bitmapped_display_controller_a6b94fdf3692083a41291eec4920c62d2_html_a6b94fdf3692083a41291eec4920c62d2"><div class="ttname"><a href="classfabgl_1_1_bitmapped_display_controller_a6b94fdf3692083a41291eec4920c62d2.html#a6b94fdf3692083a41291eec4920c62d2">fabgl::BitmappedDisplayController::queueSize</a></div><div class="ttdeci">static int queueSize</div><div class="ttdoc">Size of display controller primitives queue.</div><div class="ttdef"><b>Definition:</b> <a href="displaycontroller_8h_source.html#l00931">displaycontroller.h:931</a></div></div>
<div class="ttc" id="afabgl_8h_html"><div class="ttname"><a href="fabgl_8h.html">fabgl.h</a></div><div class="ttdoc">This file is the all in one include file. Application can just include this file to use FabGL library...</div></div>
<div class="ttc" id="afabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8_html_a2d4571e9072b78877c36bf32bc9423f8"><div class="ttname"><a href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a></div><div class="ttdeci">#define VGA_640x480_60Hz</div><div class="ttdef"><b>Definition:</b> <a href="fabglconf_8h_source.html#l00246">fabglconf.h:246</a></div></div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Mon Aug 29 2022 13:52:04 for FabGL by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3 </li>
  </ul>
</div>
</body>
</html>
